// 单按钮弹窗
Component({
  properties: {
    modalHidden: {    //分享窗的显示隐藏
      type: Boolean,
      value: true
    },
    popupConfig: {
      type: Object,
      value: {
        title: '选择分享途径'
      }
    },
    config: {
      type: Object,
      value: {
        orderId:256478,
        type: 1,          //方案类型  1:手术方案  2:产品方案  3:分割方案
        schemeId:124,     //方案ID
        shareUrl:'https://www.baidu.com'
      }
    }
  },
  options: {
    multipleSlots: true // 在组件定义时的选项中启用多slot支持
  },
  data: {
    // 这里是一些组件内部数据  
    emileUrl: '',
    modalHidden2: true,    //绑定邮箱入口窗
    popupConfig2: {
      title: '邮箱绑定'
    },
    modalHidden3:true,  //输入绑定邮箱弹窗
    btndisabled:true,
    popupConfig3: {
      title: '请输入您要绑定的邮箱账号'
    },
    mailbox:'',       //邮箱
    inputCode:'',     //验证码
    getCodeHidden:false,
    time:60,
    modalHidden4: true,  //输入绑定邮箱弹窗
    popupConfig4: {
      title: '邮箱绑定'
    },
    modalHidden5: true,  //输入绑定邮箱弹窗
    popupConfig5: {
      title: '绑定成功'
    },
    shareCode:''    //时效性token
  },
  ready:function(){
    this.getTimeToken()
    console.log(this.data.shareCode)
  },
  methods: {
    //获取时效性token
    getTimeToken:function(){
      let postUrl = wx.envConfig.host + '/order/shareToken?cookiez=' + wx.getStorageSync('cookiez')
      wx.request({
        url: postUrl + '&params=' + JSON.stringify({
          "order_id": this.data.config.orderId
        }),
        method: 'POST',
        success: (re) => {
          if (re.data.error_code === 'Y10000') {
            this.setData({
              shareCode: re.data.datas
            })
          }
        },
        fail: function (res) {}
      })
    },
    //获取用户信息
    getUserInfo:function(){
      let postUrl = wx.envConfig.host + '/user/info?cookiez=' + wx.getStorageSync('cookiez')
      let porimsEvent
      let porimsReject
      wx.request({
        url: postUrl,
        method: 'POST',
        success: (re) => {
          console.log('用户详情--------',re)
          if (re.data.error_code === 'Y10000') {
            porimsEvent(re.data.datas)
          } else {
            porimsReject(re.data)
          }
        },
        fail: function (res) {
          porimsReject(res)
        }
      })
      return new Promise((resolve, reject) => {
        porimsEvent = resolve
        porimsReject = reject
      })
    },
    //邮箱分享按钮
    shareEmile: function () {
      this.getUserInfo().then((res) => {
        if (!res.email) {
          //当未绑定邮箱，打开绑定邮箱入口
          this.setData({
            modalHidden: true,
            modalHidden2: false
          })
        }else{
          //绑定了邮箱，打开分享入口
          this.setData({
            modalHidden: true,
            modalHidden4: false,
            mailbox: res.email
          })
        }
      },(re)=>{})
    },
    //邮箱分享请求
    postEmileShare:function(){
      var regex = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
      if (!this.data.mailbox || !regex.test(this.data.mailbox)) {
        wx.showToast({
          title: '请填写有效的邮箱'
        })
        return
      }
      let postUrl = wx.envConfig.host + '/order/sendSchemeFile?cookiez=' + wx.getStorageSync('cookiez')
      wx.showLoading({
        title: '分享中',
        mask:true
      })
      wx.request({
        url: postUrl + '&params=' + JSON.stringify({
          "email": this.data.mailbox,
          "scheme_id": this.data.config.schemeId
        }),
        method: 'POST',
        success: (re) => {
          wx.hideLoading()
          if (re.data.error_code === 'Y10000') {
            wx.showToast({
              title:'分享成功',
              icon:'success'
            })
            this.setData({
              modalHidden4: true,
              modalHidden5: true
            })
          } else {
            wx.showToast({
              title: re.data.msg
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          wx.showToast({
            title: '服务器异常'
          })
        }
      })
    },
    //去绑定
    goBinding:function(){
      this.setData({
        modalHidden2: true,
        modalHidden3: false
      })
    },
    inputText: function (e) {
      this.data.mailbox = e.detail.value
      // this.setData({
      //   mailbox: e.detail.value
      // })
      this.queryInput()
    },
    inputText2: function (e) {
      this.data.inputCode = e.detail.value
      // this.setData({
      //   inputCode: e.detail.value
      // })
      this.queryInput()
    },
    //点击获取验证码
    clickGetCode:function(){
      var regex = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
      console.log(regex.test(this.data.mailbox))
      if (!this.data.mailbox || regex.test(this.data.mailbox)){
        wx.showToast({
          title:'请填写有效的邮箱'
        })
        return
      }
      this.getCode().then((res)=>{
        if (res){
          this.setData({
            getCodeHidden: true
          })
          this.setData({
            time: 60
          })
          this.reciprocal()
        }
      })
    },
    //倒数秒数
    reciprocal:function(){
      setTimeout(() => {
        if (this.data.time != 0) {
          this.reciprocal()
          this.setData({
            time: this.data.time - 1
          })
        } else {
          this.setData({
            getCodeHidden: false
          })
        }
      }, 1000)
    },
    //获取验证码
    getCode:function(){
      let postUrl = wx.envConfig.host + '/user/requestVerifyCode?cookiez=' + wx.getStorageSync('cookiez')
      let porimsEvent
      let porimsReject
      wx.request({
        url: postUrl + '&params=' + JSON.stringify({
          "account": this.data.mailbox,
          "hadRegist":'no',
          "send":'send'
        }),
        method: 'POST',
        success: (re) => {
          if (re.data.error_code === 'Y10000') {
            porimsEvent(re.data.datas)
          } else {
            porimsReject(re.data)
          }
        },
        fail: function (res) {
          porimsReject(res)
        }
      })
      return new Promise((resolve, reject) => {
        porimsEvent = resolve
        porimsReject = reject
      })
    },
    /**
     * author：xujun
     * @desc 确定是否失效
     * */
    queryInput: function () {
      let btndisabled = true
      if (this.data.mailbox.length > 6 && this.data.inputCode.length == 6 ){
        btndisabled = false
      }
      this.setData({
        btndisabled: btndisabled
      })
    },
    /**
     * author：xujun
     * @desc 确定绑定邮箱请求(确定分享弹窗)
     * */
    bindingMailbox:function(){
      var regex = /^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$/g;
      if (!this.data.mailbox || !regex.test(this.data.mailbox)) {
        wx.showToast({
          title: '请填写有效的邮箱'
        })
        return
      }
      if (!this.data.inputCode||this.data.inputCode.length!=6){
        wx.showToast({
          title: '验证码错误'
        })
        return
      }
      let postUrl = wx.envConfig.host + '/user/securityUpdate?cookiez=' + wx.getStorageSync('cookiez')
      wx.showLoading({
        title: '绑定中',
        mask: true
      })
      wx.request({
        url: postUrl + '&params=' + JSON.stringify({
          "email": this.data.mailbox,
          "sms_code": this.data.inputCode
        }),
        method: 'POST',
        success: (re) => {
          wx.hideLoading()
          if (re.data.error_code === 'Y10000') {
            this.setData({
              modalHidden3: true,
              modalHidden5: false
            })
          } else {
            wx.showToast({
              title: re.data.msg
            })
          }
        },
        fail: function (res) {
          wx.hideLoading()
          wx.showToast({
            title: '服务器异常'
          })
        }
      })
    },
    //关闭回调
    _closeback: function () {
      this.triggerEvent('cancelEvent')
    }
  }
})  